"""
@Date   : 2018/10/31 16:16
@Author : Jack
@Content: 双均线基本策略1
fix man : hashaki
last change on 2018/11/24
"""

from hashakisea.strategy.strategyTamplate import SrategyTamplate
#from hashakisea.signal.indicators.indicator import sma
from numpy.random import randn
# --------------------------------------------------------------
class DoubleMA1Strategy(SrategyTamplate):
    """双均线策略1"""
    # ----------------------------------------------------------
    def __init__(self, engine, setting):
        """构造函数, 初始化时输入实例化的引擎和参数字典"""
        super(DoubleMA1Strategy, self).__init__(engine, setting)
        # 策略基本信息
        self.className = "DoubleMA1Strategy"          # 策略类名称
        self.strategy_name = "strategy_doubleMA1"     # 策略名称

        # 策略参数
        self.n1 = setting["n1"]            # 短期均线参数
        self.n2 = setting["n2"]            # 长期均线参数
        self.fixedSize = setting["fixedSize"] # 每次买入固定手数

        # 指标变量
        pass

        # 委托单变量
        self.orderList = []

    # ----------------------------------------------------------
    def start(self):
        """策略开始"""
        # 订阅K线(品种,周期,数量)
        pass

    # ----------------------------------------------------------
    def next(self, bars):
        """K线推送"""
        bar=list(bars.values())[0]
        target_symbol=list(bars.keys())[0]
        # 撤销之前的委托单
        for orderID in self.orderList:
            self.cancel(target_symbol,orderID)
        self.orderList = []

        # 获取历史K线 TypeError: 'float' object cannot be interpreted as an integer
        #data1 = array(self.getHistoryData(self.symbol, self.frequency, num=self.n1))
        #data2 = array(self.getHistoryData(self.symbol, self.frequency, num=self.n2))
 
        # 计算均线
        ma1 = randn(1)#sma(data1)
        ma2 = randn(1)#sma(data2)

        # 计算信号
        trend_up = ma1 > ma2
        trend_dn = ma1 < ma2

        # 交易判断
        # 当前为空仓
        if self.pos[target_symbol] == 0:
            # 开多的情况
            if trend_up:
                self.orderList.append(self.buy(target_symbol,bar.close + 50, self.fixedSize, stop=False))
            # 开空的情况
            #elif trend_dn:
                #self.orderList.append(self.short(bar.close - 50, self.fixedSize, stop=False))

        elif self.pos[target_symbol] > 0:
            # 由多转空
            if trend_dn:
                self.orderList.append(self.sell(target_symbol,bar.close - 50, self.fixedSize, stop=False))  # 平多
                #self.orderList.append(self.short(bar.close - 50, self.fixedSize, stop=False)) # 开空

        #elif self.pos < 0:
            # 由空转多
            #if trend_up:
                #self.orderList.append(self.cover(bar.close + 50, self.fixedSize, stop=False)) # 平空
                #self.orderList.append(self.buy(bar.close + 50, self.fixedSize, stop=False))   # 开多

    # ----------------------------------------------------------
    def onOrder(self, order):
        """委托单回调"""
        pass

    def onTrade(self, trade):
        """成交回报回调"""
        pass



